#include <stdint.h>
#include "gic.h"
#include "task.h"
#include "uart.h"
#include "init.h"
#include "tick.h"
static void test0(void* argc)
{
    (void)argc;
    while(1){
        uart_puts("test0\n");    
        delayms_tick(30 );
    }
}

static void test1(void* argc)
{
    (void)argc;
    while(1){
        uart_puts("test1\n");    
        delayms_tick(50 );
    }
}







#define CCM_CCSR             (*(volatile uint32_t *)0x020C4000)
#define CCM_ANALOG_PLL_ARM   (*(volatile uint32_t *)0x020C8000)
#define CCM_CACRR            (*(volatile uint32_t *)0x020C4010)
#define CCM_CBCDR            (*(volatile uint32_t *)0x020C4014)

void configure_clock() {
    CCM_CCSR &= ~(1 << 2);

    CCM_ANALOG_PLL_ARM &= ~(1 << 13);  
    CCM_ANALOG_PLL_ARM = (0x1F << 0) | (0x1 << 19);  // DIV_SELECT = 31, POST_DIV_SELECT = 1
    CCM_ANALOG_PLL_ARM |= (1 << 13);  
    while (!(CCM_ANALOG_PLL_ARM & (1 << 31)));  

    CCM_CACRR = (0x1 << 0); 
    CCM_CBCDR = (0x1 << 10);
    CCM_CBCDR = (0x3 << 8); 

    CCM_CCSR |= (1 << 2);
}
void main(void) 
{
    taskParam taskParam;
    taskParam.taskId = 0;
    taskParam.priority = 10;
    taskParam.stackSize = 1024;
    taskParam.entry = test0;
    configure_clock();
    gic_init();
    gic_enable_irq(29);
    gic_set_priority(29, 0x80);
    uart_init();
    uart_puts("----start---\n");    
    osInit();
    createTask(&taskParam,"test0");
    taskParam.priority = 20;
    taskParam.entry = test1;
    createTask(&taskParam,"test1");
    
    startOs();
}

